package com.nf.health.app.healthstate.activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.Gravity;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;

import com.nf.health.app.R;
import com.nf.health.app.activity.global.BaseActivity;
import com.nf.health.app.healthstate.fragment.MyHealthyVpFragment;
import com.nf.health.app.models.SchemeCaterogy;
import com.nf.health.app.utils.ViewUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName: AddHealthyPlanActivity
 * @Description: 健康方案
 * @author Ian
 * @date 2015-11-4 上午9:38:25
 *
 */
public class AddHealthyPlanActivity extends BaseActivity {
	private ViewPager viewpager;
	private int TOTAL_PAGE;
	//	private AddHealthyPlanHeadAdapter addPlanAdapter;
//	private HorizontalListView head_sport;
	public static List<SchemeCaterogy> categories;
	private RadioGroup rg_healht_scheme_title;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setTitleBar(R.layout.titlebar_base);
		setContent(R.layout.activity_add_healthy_plan);

		ViewUtil.setText(this, R.id.titlebar_title_tv, "健康方案");
		viewpager = (ViewPager) findViewById(R.id.vp_sport);
		rg_healht_scheme_title = ViewUtil.getView(this, R.id.rg_healht_scheme_title);
//		indicator = (TabPageIndicator) findViewById(R.id.indicator);
		// 获取方案类型
		provider.requestchemeCategoryList("SchemeCategoryList");
	}
	class MyVpAdapter extends FragmentPagerAdapter {
		private List<Fragment> fragments;
		public MyVpAdapter(FragmentManager fm, List<Fragment> fragments) {
			super(fm);
			this.fragments = fragments;
		}
		@Override
		public Fragment getItem(int position) {
			return fragments.get(position);
		}

		@Override
		public int getCount() {
			return TOTAL_PAGE;
		}
	}


	@SuppressWarnings("unchecked")
	@Override
	public void handleActionSuccess(String actionName, Object object) {
		super.handleActionSuccess(actionName, object);
		if (actionName.equals("SchemeCategoryList")) {
			categories = (List<SchemeCaterogy>) object;
			TOTAL_PAGE = categories.size();

			List<Fragment> fragments = new ArrayList<Fragment>();
			for(int i = 0;i< TOTAL_PAGE;i++){
				MyHealthyVpFragment fragment = new MyHealthyVpFragment();
				Bundle bundle = new Bundle();
				bundle.putInt(MyHealthyVpFragment.KEY, i);
				fragment.setArguments(bundle);
				fragments.add(fragment);
			}
			MyVpAdapter adapter = new MyVpAdapter(getSupportFragmentManager(), fragments);
			viewpager.setAdapter(adapter);

			for (int i = 0; i < categories.size(); i++) {
				RadioButton rb = getRB(categories.get(i).getCategoryname());
				rg_healht_scheme_title.addView(rb);
				if(i == 0){
					rb.setChecked(true);
				}
			}
			initListener();
		}
	}

	private void initListener() {
		viewpager.setOnPageChangeListener(new OnPageChangeListener() {

			@Override
			public void onPageSelected(int position) {
				RadioButton rb = (RadioButton) rg_healht_scheme_title.getChildAt(position);
				rb.setChecked(true);

			}

			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {

			}

			@Override
			public void onPageScrollStateChanged(int arg0) {

			}
		});
		rg_healht_scheme_title.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				int count = rg_healht_scheme_title.getChildCount();
				for (int i = 0; i < count; i++) {
					RadioButton rb = (RadioButton) rg_healht_scheme_title.getChildAt(i);
					if (rb.isChecked()) {
						viewpager.setCurrentItem(i);
						break;
					}
				}

			}
		});
	}

	private RadioButton getRB(String name){
		RadioButton rb_title = new RadioButton(this);
		rb_title.setText(name);
		rb_title.setTextSize(16);


		rb_title.setTextColor(getResources().getColorStateList(R.color.blackw_color_day));

		rb_title.setBackgroundResource(R.drawable.rb_category_tab_title);
		rb_title.setButtonDrawable(android.R.color.transparent);
		rb_title.setPadding(16, 8, 16, 8);
		rb_title.setGravity(Gravity.CENTER);
		return rb_title;
	}
}
